#include "FormQuestion.h"

const char* comp[] =
{
		" ", ">=", "<=", ">", "<"
};

const char* opcao[] =
{
		" ", "E", "OU"
};

const char* tipos[] =
{
		"Valor inteiro", "Valor real", "Texto", "Booleano"
};

FormQuestion::FormQuestion(QWidget *parent)
    : QGroupBox(parent)
{
	ui.setupUi(this);
	setMinimumSize(935, 60);

	ui.remove->setCheckState(Qt::Unchecked);
	ui.pergunta->setText("Digite aqui a pergunta");
	ui.nomeDaVar->setText("");

	ui.tipo->addItem("Valor Inteiro");
	ui.tipo->addItem("Valor Real");
	ui.tipo->addItem("Texto");
	ui.tipo->addItem("Booleano");

	ui.op1->addItem(comp[0]);
	ui.op1->addItem(comp[1]);
	ui.op1->addItem(comp[2]);
	ui.op1->addItem(comp[3]);
	ui.op1->addItem(comp[4]);

	ui.val1->setValue(0);

	ui.opcao->addItem(opcao[0]);
	ui.opcao->addItem(opcao[1]);
	ui.opcao->addItem(opcao[2]);

	ui.op2->addItem(comp[0]);
	ui.op2->addItem(comp[1]);
	ui.op2->addItem(comp[2]);
	ui.op2->addItem(comp[3]);
	ui.op2->addItem(comp[4]);

	ui.val2->setValue(0);
}

TIPOS FormQuestion::getType(void)
{
	return (TIPOS) ui.tipo->currentIndex();
}

QString FormQuestion::getQuestion(void)
{
	return ui.pergunta->text();
	QString s;
}

FormQuestion::~FormQuestion()
{
	// I'M A BANANA
}

QTextStream& operator<<(QTextStream& s, const FormQuestion& fq)
{
	int checked;
	QString pergunta;
	QString nomeDaVariavel;
	int tipo;
	int op1;
	int valor1;
	int opcao;
	int op2;
	int valor2;

	checked = fq.ui.remove->checkState();
	pergunta = fq.ui.pergunta->text();
	nomeDaVariavel = fq.ui.nomeDaVar->text();
	tipo = fq.ui.tipo->currentIndex();
	op1 = fq.ui.op1->currentIndex();
	valor1 = fq.ui.val1->value();
	opcao = fq.ui.opcao->currentIndex();
	op2 = fq.ui.op2->currentIndex();
	valor2 = fq.ui.val2->value();

	if(pergunta.isEmpty()) return s;
	if(nomeDaVariavel.isEmpty()) return s;

	s << checked << " ";
	s << pergunta.replace(" ", "_", Qt::CaseInsensitive) << " ";
	s << nomeDaVariavel << " ";
	s << tipo << " ";
	s << op1 << " ";
	s << valor1 << " ";
	s << opcao << " ";
	s << op2 << " ";
	s << valor2 << "\n";

	return s;
}

QTextStream& operator>>(QTextStream& s, const FormQuestion& fq)
{
	int checked;
	QString pergunta;
	QString nomeDaVariavel;
	int tipo;
	int op1;
	int valor1;
	int opcao;
	int op2;
	int valor2;

	s >> checked;
	s >> pergunta;
	pergunta.replace("_", " ", Qt::CaseInsensitive);
	s >> nomeDaVariavel;
	s >> tipo;
	s >> op1;
	s >> valor1;
	s >> opcao;
	s >> op2;
	s >> valor2;

	fq.ui.remove->setCheckState((Qt::CheckState)checked);
	fq.ui.pergunta->setText(pergunta);
	fq.ui.nomeDaVar->setText(nomeDaVariavel);
	fq.ui.tipo->setCurrentIndex(tipo);
	fq.ui.op1->setCurrentIndex(op1);
	fq.ui.val1->setValue(valor1);
	fq.ui.opcao->setCurrentIndex(opcao);
	fq.ui.op2->setCurrentIndex(op2);
	fq.ui.val2->setValue(valor2);

	return s;
}
